/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package r1A2009;

import java.util.Arrays;
import java.util.HashSet;
import java.io.IOException;
import static chanlib.Chan_IO.*;

/**
 * @author robit
 */
public class MultiBaseHappy{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C

    static int square(int x,int base){
         int sum=0;
         while(x>0){
                sum+=(x%base)*(x%base);
                x/=base;
         }
         return sum;
    }

    /*
       One calculation fits all of the cases.
    */
    static int MAX=12000000;
    static int sqr[][];
    static boolean happy[][];
    static void preReckon(){
        sqr=new int[11][MAX+1];
        happy=new boolean[11][MAX+1];
        int NOTVISITED=-1,HAPPY=-2,UNHAPPY=-3;

        boolean seeone;
        for(int b=2;b<11;b++)
        {
            Arrays.fill(sqr[b],NOTVISITED);
            //loop through base
            for(int x=1;x<=MAX;x++){
                if(sqr[b][x]!=NOTVISITED) continue;
                seeone=false;
                int st=x;
                while(sqr[b][st]==NOTVISITED){// First, create a small local network.
                    st = sqr[b][st]=square(st,b);
                    if(st==1)seeone=true;
                }
                if(sqr[b][st]<0){//  previous calculation found
                    int fnd=sqr[b][st];
                    st=x;
                    while(sqr[b][st]>0){ // loop through just established local network.
                        int tmp=sqr[b][st];
                        sqr[b][st]=fnd;
                        happy[b][st]=fnd==HAPPY;
                        st=tmp;
                    }
                }else{
                    st=x;
                    int fnd=seeone?HAPPY:UNHAPPY;
                    while(sqr[b][st]>0){ // loop through just established local network.
                        int tmp=sqr[b][st];
                        sqr[b][st]=fnd;
                        happy[b][st]=seeone;
                        st=tmp;
                    }
                }
            }
        }

//        for(int i=1;i<=1000;i++){
//            String c=String.format("at %d is %b %b",i,testBelowThoudson(i, 3),happy[3][i]);
//            System.out.println(c);
//        }

    }

    /*
      calculate the answer
    */
    private static void reckon(int line) {
        String raw=Line();
        String str[]=raw.split(" ");
        int bases[]=new int[str.length];
        for(int i=0;i<bases.length;i++){
            bases[i]=Integer.parseInt(str[i]);
        }
        boolean prompt=false;
        int t=1;
        while(!prompt){
            if(t==MAX)System.exit(-1);
            t++;
            prompt=true;
            for(int b=0;b<bases.length;b++){
                //prompt&=testBelowThoudson(t, bases[b]);
                //prompt&=testAboveThoundsand(t, bases[b]);
                prompt&=happy[bases[b]][t];
            }
        }
        //if(t==MAX)System.exit(-1);
        int ans = t;

        String output=String.format("Case #%d: %d",line,ans);
        writeln(output, OUTFILE);
        //writeln(String.format("N=%d K=%d\n%s", N, K, ans),ANALFILE);
        //println(output);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="A";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            initIO(filename);
            int lineNums = Integer.parseInt(Line());
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
            }
        } finally {
            endIO();
        }
    }

}
